/* 
 * Copyright (C) 2012 Martin Řehánek <rehan at mzk.cz>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

goog.provide('kramerius.audio.Playlist');

kramerius.audio.Playlist = function(){
    this.tracks_ = [] ;
}

kramerius.audio.Playlist.prototype.getTrackAtPosition_ = function(position){
    if (position >= this.tracks_.length){
        console.log("position (" + position + ") >= " + " playlist length (" + this.tracks.length + ")");
        return 0;
    }else{
        return this.tracks_[position];
    }
}

kramerius.audio.Playlist.prototype.play = function(playlistId){
    //If SoundManager object exists, get rid of it...
    if (this.nowPlaying){
        this.nowPlaying.destruct();
        // ...and reset array key if end reached
        if(playlistId == this.tracks_.length){
            playlistId = 0;
        }
    }
    
    var track = this.getTrackAtPosition_(playlistId);
    var playlist = this;
    console.log("playing track : " + track.title + ", " + track.trackArray());
    soundManager.onready(function() {
        playlist.nowPlaying = soundManager.createSound({
            id: track.getTitle(),
            url: track.trackArray(),
            autoLoad: true,
            autoPlay: true,
            volume: 50,
            multiShotEvents: true,
            // ...with a recursive callback when play completes
            onfinish: function(){
                playlistId ++;
                //console.log('moving to next track: ' + playlistId);
                //    tracks.play(playlistId);
                playlist.play(playlistId);
            }
        });
    });
} 





